<template>
    <div>
        <el-form :model="form" ref="form" label-width="100px">
            <el-form-item label="入库说明" prop="name" :rules="{ required: true, message: '请输入入库说明', trigger: 'blur' }">
                <el-input v-model.trim="form.name" auto-complete="off" placeholder="请输入入库说明" clearable></el-input>
            </el-form-item>
            <el-form-item label="配件" prop="part_id" :rules="{ required: true, message: '请选择配件', trigger: 'blur' }">
                <!-- <el-cascader
                    v-model="form.part_id"
                    :options="partList"
                    :props="{ emitPath: false, value: 'id', label: 'name', children: 'Parts' }"
                    placeholder="配件"
                    clearable
                    filterable
                    style="width:400px"
                >
                    <template slot-scope="{ node, data }">
                        <span v-if="!node.isLeaf">{{ `${data.type} ${data.name} (${data.Parts.length})` }}</span>
                        <span v-else>{{ `${data.name} ${data.no} ${data.model}` }}</span>
                    </template>
                </el-cascader>-->
                <el-select v-model="form.part_id" placeholder="请选择配件" filterable clearable style="width:600px">
                    <el-option
                        v-for="item in partList"
                        :key="item.id"
                        :label="`${item.name}, 货号:${item.no}, 规格:${item.model}, 分类:${item.Category.type}-${item.Category.name}`"
                        :value="item.id"
                    ></el-option>
                </el-select>
            </el-form-item>
            <el-form-item
                label="入库数量"
                prop="count"
                :rules="[
                    { required: true, message: '数量不能为空', trigger: 'blur' },
                    { type: 'integer', min: 1, message: '入库数量不能小于 1', trigger: 'blur'}
                ]"
            >
                <el-input-number v-model="form.count" controls-position="right" :min="1"></el-input-number>
            </el-form-item>
            <el-form-item label="入库日期" prop="date" :rules="{ required: true, message: '请选择入库日期', trigger: 'blur' }">
                <el-date-picker type="date" v-model="form.date" format="yyyy 年 MM 月 dd 日" value-format="yyyy-MM-dd" placeholder="请选择入库日期"></el-date-picker>
            </el-form-item>
            <el-form-item>
                <el-button type="primary" @click="submit('form')">保 存</el-button>
                <el-button @click="$router.push({ name: from })">返 回</el-button>
            </el-form-item>
        </el-form>
    </div>
</template>

<script>
import dayjs from '../../config/day.js'
export default {
    props: { id: null },
    data() {
        return {
            from: this.$route.query.from || 'partStoreList',
            form: {
                name: null,
                date: dayjs().format('YYYY-MM-DD'),
                count: 1,
                part_id: this.$route.query.part_id
            },
            partList: []
        }
    },
    async created() {
        const response = await this.$http.get('/partList')
        if (response && response.code === 0) {
            this.partList = response.returnObject
        }
        this.id && this.fetch()
    },
    methods: {
        async fetch() {
            const response = await this.$http.get(`/partPurchase/${this.id}`)
            if (response && response.code === 0) {
                this.form = response.returnObject
            }
        },
        submit(formName) {
            this.$refs[formName].validate(async valid => {
                if (!valid) return false
                if (this.id) {
                    const response = await this.$http.put(`/partPurchase/${this.id}`, this.form)
                    if (response && response.code === 0) {
                        this.$message.success('修改成功')
                        this.$router.push({ name: this.from })
                    }
                } else {
                    const res = await this.$http.post('/partPurchase', this.form)
                    if (res && res.code === 0) {
                        this.$message.success('添加成功')
                        this.$router.push({ name: this.from })
                    }
                }
            })
        }
    }
}
</script>

<style scoped></style>
